//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Spreadsheet.Expressions;

namespace OpenLS.Spreadsheet.Parsing
{
    class MultiSheetRangeReferenceExpression : MultiSheetExpression
    {
        private readonly List<ReferenceToken> _tokens;

        public MultiSheetRangeReferenceExpression(List<Worksheet> worksheets, List<ReferenceToken> tokens): base (worksheets)
        {
            _tokens = tokens;
        }
        public override IEnumerable<Expression> GetSubExpressions()
        {
            yield break;
        }
        public override string GetText(TextContext context)
        {
            var namingConvention = _worksheets[0].Workbook.NamingConvention;
            return WorksheetsText + "!" + StringEnumerable.Concat(_tokens, tt => tt.GetText(namingConvention), ":");
        }
        internal override Expression Transposed(Cell target)
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<IDependencyNode> GetPrecedents(EvaluationContext context)
        {
            foreach (var worksheet in _worksheets)
            {
                foreach (var token in _tokens)
                {
                    if (token is CellReferenceToken)
                    {
                        CellReferenceToken ct = (CellReferenceToken) token;
                        yield return ct.Project(worksheet).Offset(context.Offset).Cell;
                        continue;
                    }
                    RangeReferenceToken rt = (RangeReferenceToken) token;
                    new RangeNode(rt.Project(worksheet).Offset(context.Offset).Range);
                }
            }
        }

        protected internal override ICellEnumerator GetCellsValue(EvaluationContext context)
        {
            throw new NotImplementedException();
        }

        public override Expression Offset(CellRelativeAddress a)
        {
            throw new NotImplementedException();
        }
    }
}